
var util = require('../../utils/util.js');
var api = require('../../config/api.js');
const user = require('../../services/user.js');

var app = getApp();
var isOperate = 0;

Page({
  data: {
    array: ['微信退款'],
    index: 0,
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  onLoad: function (options) {
    var that = this;
    var orderId = options.orderId;
    that.setData({
      orderId: orderId
    });
    var data = {
      orderId: orderId
    }
    util.request(api.PinglunInfo, data, 'POST').then(function (data) {
      if (data.type == 'success') {
        that.setData({
          dataList: data.data.dataList
        });
      } else {
        console.log(data.content);
      }
    });
  },
  //提交评价
  addPinglun: function () {
    if (isOperate == 1) {
      return false;
    }
    isOperate = 1;
    var orderId = this.data.orderId;
    var dataList = this.data.dataList;
    for (var index = 0; index < dataList.length; index++) {
      if (dataList[index].content == null || dataList[index].content == '') {
        util.showSuccessToast('请填写评论内容');
        isOperate = 0;
        return false;
      }
      var imageList = dataList[index].imageList;
      // if (imageList.length <= 0) {
      //   util.showSuccessToast('请上传评价图片');
      //   isOperate = 0;
      //   return false;
      // }
      if (imageList.length > 3) {
        util.showSuccessToast('单个商品请上传少于四张图片');
        isOperate = 0;
        return false;
      }
    }
    var assessStr = JSON.stringify(dataList);
    var data = {
      assessStr: assessStr,
      orderId: orderId
    }
    util.request(api.PinglunAdd, data, 'POST').then(function (data) {
      if (data.type == 'success') {
        util.showSuccessToast(data.content);
        setTimeout(function () {
          wx.navigateBack();
        }, 1500)
      } else {
        console.log(data.content);
        isOperate = 0;
      }
    });
  },
  bindinputContent: function (event) {
    var dataList = this.data.dataList;
    var index = event.currentTarget.dataset.index;
    dataList[index].content = event.detail.value;
    this.setData({
      dataList: dataList
    });
  },
  //上传图片
  chooseImage: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        var filePath = res.tempFilePaths[0];
        wx.uploadFile({
          url: api.UploadFile,
          filePath: filePath,
          name: 'file',
          header: { 'Content-Type': 'application/x-www-form-urlencoded' },
          success: function (res) {
            if (JSON.parse(res.data).type == 'success') {
              var dataList = that.data.dataList;
              dataList[index].imageList = dataList[index].imageList.concat(JSON.parse(res.data).data.absolute);
              that.setData({
                dataList: dataList
              });
            }
          },
          fail: function ({ errMsg }) {
            console.log('uploadImage fail, errMsg is', errMsg)
          }
        })
      }
    })
  },
  //删除图片
  delImage: function (e) {
    var index = e.currentTarget.dataset.index;
    var imageIndex = e.currentTarget.dataset.imageIndex;
    var dataList = this.data.dataList;
    var imageList = dataList[index].imageList;
    imageList.splice(imageIndex, 1);
    this.setData({
      dataList: dataList
    });
  }

}) 